Thread: Finding common values in the rows of a vector [Need Help]

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    Finding common values in the rows of a vector [Need Help]

    Can somebody give me a hand with this? I'm doing this for a an application that would print of LCM and GCD (lowest common factor and greatest common divisor) with steps.

    I need to get the common factors so I can highlight them when I'm printing them under factors. I tried googling but I could only find programs that would directly find LCM or GCD. It's important I have all the factors.

    The best I could come up with:
    Code:
    int main() {
        vector <float> operands = { 9, 9, 4 };
        vector < vector <float> > factors;
        vector < vector <float> > factors_temp;
        vector <float> common_factors;
    
        for (const auto& x : operands)
            factors.push_back(prime_factors(x)); // returns a vector 
        
        factors_temp = factors;
    
        for (unsigned int i = 0; i < factors_temp.size(); i++) {
            for (unsigned int j = 0; j < factors_temp[i].size(); j++) {
    
                for (unsigned int k = 0; k < factors_temp.size(); k++) {
                    for (unsigned int l = 0; l < factors_temp[k].size(); l++) {
                        if (k == i)
                            continue;
    
                        if (factors_temp[i][j] == factors_temp[k][l]) {
                            common_factors.push_back(factors_temp[i][j]);
                            factors_temp[i].erase(factors_temp[i].begin() + j);
                            factors_temp[k].erase(factors_temp[k].begin() + l);
                        }
                    }
                }
            }
        }
    
        for (const auto& x : common_factors)
            cout << x << "\t";
    
        cin.get();
        return 0;
    }
    The logic is completely faulty though. It occasionally works for two operands.

    Oh and also I need to erase common factors from the rows and columns themselves. (So they won't repeat while printing out, unless there is a better logic to this)

    Thanks for reading, cheers!


    here's the other part of the code:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <Windows.h>
    
    using namespace std;
    
    void SetColor(int ForgC) // source: https://stackoverflow.com/questions/29574849/how-to-change-text-color-and-console-color-in-codeblocks
    {
        WORD wColor;
    
        HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO csbi;
    
        //We use csbi for the wAttributes word.
        if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
        {
            //Mask out all but the background attribute, and add in the forgournd     color
            wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
            SetConsoleTextAttribute(hStdOut, wColor);
        }
        return;
    }
    
    vector <int> prime_factors(int number) {  // over-loaded function
        vector <int> prime_factors = { 1 };
    
        for (int i = 2; i <= number; i++) {
            while (number % i == 0) {
                number /= i;
                prime_factors.push_back(i);
            }
        }
        return prime_factors;
    }
    Last edited by Nwb; 10-05-2018 at 12:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array Problem - Finding each rows minimum value
    By Jordan Velez in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2015, 01:04 PM
  2. Finding a common suffix
    By echo1525 in forum C Programming
    Replies: 3
    Last Post: 11-12-2012, 12:35 AM
  3. finding common numbers
    By BB89 in forum C Programming
    Replies: 14
    Last Post: 11-10-2009, 08:22 AM
  4. finding most common char in a string
    By scwizzo in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 01:22 PM
  5. Finding the most common char
    By Mikro in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 09:00 AM

Tags for this Thread